<?php
class EvaluatesController extends AppController{
	var $uses=null;	
	var $helpers = array('javascript');
	//function testing($claimid, $patientid){	
	function printValidation($claimid, $patientid){
		App::import('Model', 'Patient');
		App::import('Model', 'Comment');
		App::import('Model','Evaluatedother');					
		App::import('Model','Evaluatedphysician');
		App::import('Model','Evaluatedsurgeon');
		App::import('Model','Evaluatedphysician');
		App::import('Model','Evaluatedanesthesiologist');
		$tot=0;			
		//----------------Patient------------------------
		$Patient=new Patient();
		$patient=$Patient->findById($patientid);
		
		$this->set('patient', $patient);
		//-----------------------------------------------
		//----------------Others-------------------------
		$Evalother=new Evaluatedother();
		$expense=array();
		$expenseType=array('Room & Board', 'X Rays/Lab & Others', 'O.R Fee', 'Confinement Expense');							
		
		$field=array('service_type, SUM(actual_charge) AS actual', 'SUM(claim_hospital) AS claim');
		$group=array('service_type');
		$condition=array('claimid'=>$claimid);			
		$data=$Evalother->find('all', array('fields'=>$field, 'group'=>$group, 'conditions'=>$condition));
		$total=0;
		foreach($data as $value){
			if($value['Evaluatedother']['service_type']==1){
				$expense[]=array('Benefits'=>'Room & Board', 'Actutal'=>number_format($value[0]['actual'],2), 'Computed Allowable'=>number_format($value[0]['claim'],2));
			}
			else if($value['Evaluatedother']['service_type']==2){
				$expense[]=array('Benefits'=>'X Rays/Lab & Others', 'Actutal'=>number_format($value[0]['actual'],2), 'Computed Allowable'=>number_format($value[0]['claim'],2));
			}
			else if($value['Evaluatedother']['service_type']==3){
				$expense[]=array('Benefits'=>'O.R Fee', 'Actutal'=>number_format($value[0]['actual'],2), 'Computed Allowable'=>number_format($value[0]['claim'],2));
			}
			else if($value['Evaluatedother']['service_type']==4){
				$expense[]=array('Benefits'=>'Confinement Expense', 'Actutal'=>number_format($value[0]['actual'],2), 'Computed Allowable'=>number_format($value[0]['claim'],2));
			}
			$total+=$value[0]['claim'];			
		}
		$tot+=$total;
		$expense[]=array('Benefits'=>'', 'Actutal'=>'', 'Computed Allowable'=>'Sub Total: '.number_format($total,2));
		$this->set('expense', $expense);
		//-----------------------------------------------
		//--------------Surgeon--------------------------
		$Evaluatedsurgeon=new Evaluatedsurgeon();
		$surgeon=array();
		$condition=array('claimid'=>$claimid);		
		$data=$Evaluatedsurgeon->find('all', array('conditions'=>$condition));
		$total1=0;
		foreach($data as $value){
			//echo $value['Evaluatedsurgeon']['rvs'].'<br>';
			$name=$value['Evaluatedsurgeon']['firstname'].' '.$value['Evaluatedsurgeon']['middlename'].' '.$value['Evaluatedsurgeon']['lastname'];
			$surgeon[]=array(
				'Surgeon'=>$value['Evaluatedsurgeon']['rvs'].'/'.$value['Evaluatedsurgeon']['accreditationcode'].'<br>'.$name.'<br>@'.$value['Evaluatedsurgeon']['rvu'],
				'Actual'=>number_format($value['Evaluatedsurgeon']['actualcharge'],2),
				'Computed Allowable'=>number_format($value['Evaluatedsurgeon']['benefitclaim'],2)
			);
			$total1+=$value['Evaluatedsurgeon']['benefitclaim'];
		}
		$tot+=$total1;
		$surgeon[]=array('Surgeon'=>'', 'Actual'=>'', 'Computed Allowable'=>'Sub Total: '.number_format($total1, 2));	
		$this->set('surgeon', $surgeon);
		//-----------------------------------------------	
		//--------------Anesthesiologist-----------------
		$Evaluatedanesthesiologist=new Evaluatedanesthesiologist();
		$anesthesia=array();
		$condition=array('claimid'=>$claimid);
		$data=$Evaluatedanesthesiologist->find('all', array('conditions'=>$condition));
		$total2=0;
		foreach($data as $value){
			//echo $value['Evaluatedsurgeon']['rvs'].'<br>';
			$name=$value['Evaluatedanesthesiologist']['firstname'].' '.$value['Evaluatedanesthesiologist']['middlename'].' '.$value['Evaluatedanesthesiologist']['lastname'];
			$anesthesia[]=array(
				'Anesthesiologist'=>$value['Evaluatedanesthesiologist']['rvs'].'/'.$value['Evaluatedanesthesiologist']['accreditationcode'].'<br>'.$name.'<br>@'.$value['Evaluatedanesthesiologist']['rvu'],
				'Actual'=>number_format($value['Evaluatedanesthesiologist']['actualcharge'],2),
				'Computed Allowable'=>number_format($value['Evaluatedanesthesiologist']['benefitclaim'],2)
			);
			$total2+=$value['Evaluatedanesthesiologist']['benefitclaim'];
		}
		$tot+=$total2;
		$anesthesia[]=array('Anesthesiologist'=>'', 'Actual'=>'', 'Computed Allowable'=>'Sub Total: '.number_format($total2, 2));
		$this->set('anesthesiologist', $anesthesia);
		//-----------------------------------------------
		//--------------Physician------------------------
		$Evaluatedphysician=new Evaluatedphysician();
		$physician=array();		
		$condition=array('claimid'=>$claimid);
		$data=$Evaluatedphysician->find('all', array('conditions'=>$condition));
		$total3=0;
		foreach($data as $value){
			$name=$value['Evaluatedphysician']['firstname'].' '.$value['Evaluatedphysician']['middlename'].' '.$value['Evaluatedphysician']['lastname'];
			$physician[]=array(
				'Physician'=>$value['Evaluatedphysician']['dayscovered'].'/'.$value['Evaluatedphysician']['dailyrate'].'<br>'.$name.'<br>@'.$value['Evaluatedphysician']['accreditationcode'],
				'Actual'=>number_format($value['Evaluatedphysician']['actualcharge'],2),
				'Computed Allowable'=>number_format($value['Evaluatedphysician']['benifitclaim'],2)
			);
			$total3+=$value['Evaluatedphysician']['benifitclaim'];
		}
		$tot+=$total3;
		$physician[]=array('Physician'=>'', 'Actual'=>'', 'Computed Allowable'=>'Sub Total: '.number_format($total3, 2));
		$this->set('physician', $physician);
		//-----------------------------------------------
		//--------------------Comment--------------------
		$Comment=new Comment();
		$comment=array();
		$condition=array('patient_id'=>$patientid);
		$data=$Comment->find('all', array('conditions'=>$condition));
		foreach($data as $row){
			$comment[]=array('Comment'=>$row['Comment']['comment'], 'Date'=>date('Y/m/d', strtotime($row['Comment']['date'])));
		}
		$this->set('comment', $comment);
		//-----------------------------------------------												
		$this->set('total', $tot);
		$this->layout = 'pdf';        
        $this->render();
	}
	function evaluate($patientid){
		App::import('Model','Patient');
		App::import('Model','Drug');
		App::import('Model','Other');
		App::import('Model','Evaluatedother');
		App::import('Model','Evaluateddrug');
		App::import('Model','Evaluatedphysician');
		App::import('Model','Evaluatedsurgeon');
		App::import('Model','Evaluatedanesthesiologist');
		App::import('Model','Physician');
		App::import('Model','Surgeon');
		App::import('Model','Anesthesiologist');
		App::import('Model','Rvscode');
		App::import('Model','Casetype');
		
		$patient=new Patient();
		$pat=$patient->findById($patientid);
		$diagnosis=$this->requestAction('/casetypes/getDescription/'.$pat['Patient']['admissiondiagnosis']);
		$finaldiagnosis=$this->requestAction('/casetypes/getDescription/'.$pat['Patient']['finaldiagnosis']);
		$casetype=$this->requestAction('/casetypes/getCaseType/'.$pat['Patient']['finaldiagnosis']);		
		$other=new Other();
		
		$Casetype=new Casetype();
		$physician=new Physician();		
		$surgeon=new Surgeon();
		$evaluatedsurgeon=new Evaluatedsurgeon();
		$evaluatedother=new Evaluatedother();
		$evaluatedaneshtesiologist=new Evaluatedanesthesiologist();
		$anesthesiologist=new Anesthesiologist();
		
		$casetypelist=$Casetype->find('all');		
		
		$rvscode=new Rvscode();
		
		$id=$pat['Patient']['id'];
		$claimid=$pat['Patient']['accreditation'].				
				$pat['Patient']['membernumber'].
				date('Ymd',strtotime($pat['Patient']['dateadmitted'])).
				date('Hi',strtotime($pat['Patient']['timeadmitted']))
				;				
		//--------------------drugs---------------------						
		$drug=new Drug();
		$drugEvaluated=new Evaluateddrug();
		$dataDrug=$drug->find('all',array('conditions'=>array('claimid'=>$claimid, 'status'=>1)));
		//$d=$drug->find('all',array('conditions'=>array('claimid'=>$claimid)));
		$d=$drugEvaluated->find('all',array('conditions'=>array('claimid'=>$claimid)));
		$dr=$drugEvaluated->find('all',array('conditions'=>array('claimid'=>$claimid, 'status'=>1)));
		$ahc=0;
		foreach($dataDrug as $row){
			$ahc+=$row['Drug']['actual_charge'];
		}
		$hc=0;
		foreach($dr as $row){
			$hc+=$row['Evaluateddrug']['claim_hospital'];
		}
		$pc=0;
		foreach($dataDrug as $row){
			$pc+=$row['Drug']['claim_patient'];
		}
		$this->set('drug', $d);
		$this->set('ahc', $ahc);
		$this->set('hc', $hc);
		$this->set('pc', $pc);
		//-----------------------------------------------
		//----------------------rooms and board----------
		$dataRooms=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>1, 'status'=>1)));
		//$r=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>1)));
		$r=$evaluatedother->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>1)));
		$rr=$evaluatedother->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>1, 'status'=>1)));
		$rohc=0;
		foreach($dataRooms as $row){
			$rohc+=$row['Other']['actual_charge'];
		}
		$rhc=0;
		foreach($rr as $row){
			$rhc+=$row['Evaluatedother']['claim_hospital'];
		}
		$rpc=0;
		foreach($dataRooms as $row){
			$rpc+=$row['Other']['claim_patient'];
		}
		$this->set('rohc', $rohc);
		$this->set('rhc', $rhc);
		$this->set('rpc', $rpc);
		$this->set('rooms', $r);
		$this->set('ri', 1);
		//-----------------------------------------------
		//----------------------x rays test----------
		$dataXray=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>2, 'status'=>1)));
		//$x=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>2)));
		$x=$evaluatedother->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>2)));
		$xr=$evaluatedother->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>2, 'status'=>1)));
		$xohc=0;
		foreach($dataXray as $row){
			$xohc+=$row['Other']['actual_charge'];
		}
		$xhc=0;
		foreach($xr as $row){
			$xhc+=$row['Evaluatedother']['claim_hospital'];
		}
		$xpc=0;
		foreach($dataXray as $row){
			$xpc+=$row['Other']['claim_patient'];
		}
		$this->set('xohc', $xohc);
		$this->set('xhc', $xhc);
		$this->set('xpc', $xpc);
		$this->set('xray', $x);
		$this->set('xi', 1);
		//-----------------------------------------------
		//----------------------operating room----------
		$dataOperation=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>3, 'status'=>1)));
		//$o=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>3)));
		$o=$evaluatedother->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>3)));
		$or=$evaluatedother->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>3, 'status'=>1)));
		$oohc=0;
		foreach($dataOperation as $row){
			$oohc+=$row['Other']['actual_charge'];
		}
		$ohc=0;
		foreach($or as $row){
			$ohc+=$row['Evaluatedother']['claim_hospital'];
		}
		$opc=0;
		foreach($dataOperation as $row){
			$opc+=$row['Other']['claim_patient'];
		}
		$this->set('oohc', $oohc);
		$this->set('ohc', $ohc);
		$this->set('opc', $opc);
		$this->set('operationd', $o);
		//-----------------------------------------------
		//----------------------confinement expense----------
		$dataConfinement=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>4, 'status'=>1)));
		//$c=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>4)));
		$c=$evaluatedother->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>4)));
		$cr=$evaluatedother->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>4, 'status'=>1)));
		$chc=0;
		foreach($dataConfinement as $row){
			$chc+=$row['Other']['actual_charge'];
		}
		$chc=0;
		foreach($cr as $row){
			$chc+=$row['Evaluatedother']['claim_hospital'];
		}
		$cpc=0;
		foreach($dataConfinement as $row){
			$cpc+=$row['Other']['claim_patient'];
		}
		$this->set('chc', $chc);
		$this->set('chc', $chc);
		$this->set('cpc', $cpc);
		$this->set('confinement', $c);
		//-----------------------------------------------
		//-------------------physicians expense--------
		$evalPhysician=new Evaluatedphysician();	
		//$dataphysician=$physician->find('all', array('conditions'=>array('claimid'=>$claimid)));
		$dataphysician=$evalPhysician->find('all', array('conditions'=>array('claimid'=>$claimid)));
		//$dpsssssssssss=$physician->find('all', array('conditions'=>array('claimid'=>$claimid, 'status'=>1)));		
		$dpsssssssssss=$evalPhysician->find('all', array('conditions'=>array('claimid'=>$claimid, 'status'=>1)));
		$dailyrate=$this->requestAction('/physicians/getDailyRate/', array('pass' => array($id)));
		$dpa=0;
		$dpb=0;
		foreach($dpsssssssssss as $row){
			//Evaluatedphysician
			//$dpa+=$row['Physician']['actualcharge'];
			//$dpb+=$row['Physician']['benifitclaim'];
			$dpa+=$row['Evaluatedphysician']['actualcharge'];
			$dpb+=$row['Evaluatedphysician']['benifitclaim'];
		}	
		$this->set('dpa', $dpa);
		$this->set('dpb', $dpb);
		$this->set('dprate', $dailyrate);
		$this->set('dataphysician', $dataphysician);
		//---------------------------------------------
		//--------------------surgeon expense---------
		//$datasergeon=$surgeon->find('all', array('conditions'=>array('claimid'=>$claimid)));
		$datasergeon=$evaluatedsurgeon->find('all', array('conditions'=>array('claimid'=>$claimid)));
		$ds=$surgeon->find('all', array('conditions'=>array('claimid'=>$claimid, 'status'=>1)));
		$dsevaluated=$evaluatedsurgeon->find('all', array('conditions'=>array('claimid'=>$claimid, 'status'=>1)));
		$oper=$rvscode->findAll('','','rvscode ASC');
		
		$dsa=0;
		$dsb=0;
		foreach($ds as $row){
			$dsa+=$row['Surgeon']['actualcharge'];
			//$dsb+=$row['Surgeon']['benefitclaim'];
		}
		foreach($dsevaluated as $rowf){
			$dsb+=$rowf['Evaluatedsurgeon']['benefitclaim'];
			//$dsb+=$row['Surgeon']['benefitclaim'];
		}
		$this->set('dsa', $dsa);
		$this->set('dsb', $dsb);
		$this->set('operation', $oper);
		$this->set('datasergeon', $datasergeon);
		//---------------------------------------------		
		//------------------anesthesilogist------------
		//$evaluatedaneshtesiologist
		//$dataanesthesiologist=$anesthesiologist->find('all', array('conditions'=>array('claimid'=>$claimid)));
		$dataanesthesiologist=$evaluatedaneshtesiologist->find('all', array('conditions'=>array('claimid'=>$claimid)));
		$da=$anesthesiologist->find('all', array('conditions'=>array('claimid'=>$claimid, 'status'=>1)));
		$aevalauted=$evaluatedaneshtesiologist->find('all', array('conditions'=>array('claimid'=>$claimid, 'status'=>1)));		
		$daa=0;
		$dab=0;
		foreach($da as $row){
			$daa+=$row['Anesthesiologist']['actualcharge'];
			//$dab+=$row['evaluatedaneshtesiologist']['benefitclaim'];
		}
		foreach($aevalauted as $row){
			//$daa+=$row['Anesthesiologist']['actualcharge'];
			$dab+=$row['Evaluatedanesthesiologist']['benefitclaim'];
		}
		$this->set('daa',$daa);
		$this->set('dab',$dab);
		//---------------------------------------------
		
		$this->set('data', $pat);
		$this->set('di', 1);
		
		
		$this->set('patientid', $patientid);
		$this->set('claimid', $claimid);
		$this->set('diagnosis', $diagnosis);
		$this->set('finaldiagnosis', $finaldiagnosis);
		$this->set('casetype', $casetype);				
		$this->set('dataanesthesiologist', $dataanesthesiologist);
		$this->set('casetypelist', $casetypelist);
	}
	function printEvaluate($claimid, $patientid){
		App::import('Model','Patient');		
		App::import('Model','Drug');
		App::import('Model','Other');
					
		$patient=new Patient();
		$other=new Other();
		$drug=new Drug();
				
		$totalcharge=0;
		$totalhospital=0;
		$totalpatient=0;
		$pat=$patient->findById($patientid);		
		
		$this->set('data', $pat);
		
		//----------------------rooms and board----------
		$dataRooms=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>1, 'status'=>1)));
		$r=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>1)));
		$rohc=0;
		foreach($dataRooms as $row){
			$rohc+=$row['Other']['actual_charge'];
		}
		$totalcharge+=$rohc;
		$rhc=0;
		foreach($dataRooms as $row){
			$rhc+=$row['Other']['claim_hospital'];
		}
		$totalhospital+=$rhc;
		$rpc=0;
		foreach($dataRooms as $row){
			$rpc+=$row['Other']['claim_patient'];
		}
		$totalpatient+=$rpc;
		$this->set('rohc', $rohc);
		$this->set('rhc', $rhc);
		$this->set('rpc', $rpc);
		$this->set('rooms', $r);
		//-----------------------------------------------------
		//--------------------drugs---------------------
		$dataDrug=$drug->find('all',array('conditions'=>array('claimid'=>$claimid, 'status'=>1)));
		$d=$drug->find('all',array('conditions'=>array('claimid'=>$claimid)));
		$ahc=0;
		foreach($dataDrug as $row){
			$ahc+=$row['Drug']['actual_charge'];
		}
		$totalcharge+=$ahc;
		$hc=0;
		foreach($dataDrug as $row){
			$hc+=$row['Drug']['claim_hospital'];
		}
		$totalhospital+=$hc;
		$pc=0;
		foreach($dataDrug as $row){
			$pc+=$row['Drug']['claim_patient'];
		}
		$totalpatient+=$pc;
		$this->set('ahc', $ahc);
		$this->set('hc', $hc);
		$this->set('pc', $pc);
		//-----------------------------------------------
		//----------------------x rays test----------
		$dataXray=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>2, 'status'=>1)));
		$x=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>2)));
		$xohc=0;
		foreach($dataXray as $row){
			$xohc+=$row['Other']['actual_charge'];
		}
		$totalcharge+=$xohc;
		$xhc=0;
		foreach($dataXray as $row){
			$xhc+=$row['Other']['claim_hospital'];
		}
		$totalhospital+=$xhc;
		$xpc=0;
		foreach($dataXray as $row){
			$xpc+=$row['Other']['claim_patient'];
		}
		$totalpatient+=$xpc;
		$this->set('xohc', $xohc);
		$this->set('xhc', $xhc);
		$this->set('xpc', $xpc);
		$this->set('xray', $x);		
		//-----------------------------------------------
		//----------------------operationg room----------
		$dataOperation=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>3, 'status'=>1)));
		$o=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>3)));
		$oohc=0;
		foreach($dataOperation as $row){
			$oohc+=$row['Other']['actual_charge'];
		}
		$totalcharge+=$oohc;
		$ohc=0;
		foreach($dataOperation as $row){
			$ohc+=$row['Other']['claim_hospital'];
		}
		$totalhospital+=$ohc;
		$opc=0;
		foreach($dataOperation as $row){
			$opc+=$row['Other']['claim_patient'];
		}
		$totalpatient+=$opc;
		$this->set('oohc', $oohc);
		$this->set('ohc', $ohc);
		$this->set('opc', $opc);
		$this->set('operation', $o);
		//-----------------------------------------------
		//----------------------confinement expense----------
		$dataConfinement=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>4, 'status'=>1)));
		$c=$other->find('all',array('conditions'=>array('claimid'=>$claimid, 'service_type'=>4)));
		$chc=0;
		foreach($dataConfinement as $row){
			$chc+=$row['Other']['actual_charge'];
		}
		$totalcharge+=$chc;
		$chc1=0;
		foreach($dataConfinement as $row){
			$chc1+=$row['Other']['claim_hospital'];
		}
		$totalhospital+=$chc1;
		$cpc=0;
		foreach($dataConfinement as $row){
			$cpc+=$row['Other']['claim_patient'];
		}
		$totalpatient+=$cpc;
		$this->set('chc', $chc);
		$this->set('chc1', $chc1);
		$this->set('cpc', $cpc);
		$this->set('confinement', $c);
		//-----------------------------------------------
		
		$this->set('totalactual', $totalcharge);
		$this->set('totalhospital', $totalhospital);
		$this->set('totalpatient', $totalpatient);
		
		$this->layout = 'pdf';        
        $this->render();
	}
}
?>